package com.songlanyun.modules.rabbitmq.mq.consumer;

import com.rabbitmq.client.Channel;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyTeam;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyTeamService;
import com.songlanyun.modules.rabbitmq.DelayedConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 过期没有成团的团需要取消
 */
@Component
@Slf4j
public class AutoExpireTeamConsumer extends BaseConsumer {

    @Autowired
    private GroupBuyTeamService groupBuyTeamService;


    @Override
    protected void process(Message message) {
        Long teamId = ClassConvertUtils.objToLong(new String(message.getBody()));
        GroupBuyTeam v = groupBuyTeamService.getById(teamId);
        try {
            log.info(String.format("编号:%s 过期没有组团成功，开始执行取消", v.getId().toString()));
            groupBuyTeamService.expireTeamHandle(v);
            log.info(String.format("编号:%s 过期没有组团成功，执行取消完成", v.getId().toString()));
        }
        catch (Exception ex) {
            log.error(String.format("编号:%s 过期没有组团成功，执行取消,出错%s", v.getId().toString(), ex.getMessage()));
            throw ex;
        }
    }

    @RabbitListener(queues = DelayedConstant.AutoExpireTeamConstant.AUTO_EXPIRE_TEAM_DELAYED_QUEUE)
    public void consume(Message message, Channel channel) throws IOException {
        super.handle(message,channel);
    }
}
